package linked_list;
//给定一个头节点为 head 的链表用于记录一系列核心肌群训练项目编号，请查找并返回倒数第 cnt 个训练项目编号。
public class 训练计划2 {
     public class ListNode {
          int val;
          ListNode next;
          ListNode() {}
          ListNode(int val) { this.val = val; }
          ListNode(int val, ListNode next) { this.val = val; this.next = next; }
      }

    //快慢指针
    class Solution {
        public ListNode trainingPlan(ListNode head, int cnt) {
            ListNode pre = head;
            ListNode cur = head;
            while(pre != null){
                if(cnt <= 0)
                    cur = cur.next;
                pre = pre.next;
                cnt--;
            }
            return cur;
        }
    }
    class Solution1 {
         public ListNode trainingPlan(ListNode head, int k) {
             ListNode fast = head;
             ListNode low = head;
             while(k-- > 0){
                 fast = fast.next;
             }
             while (fast != null){
                 fast = fast.next;
                 low = low.next;
             }
             return low;
         }
    }
}
